package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortPoint {
    public static void main(String[] args) {
        //
        List<Point> list=new ArrayList<>();
        list.add(new Point(1,2));
        list.add(new Point(3,4));
        list.add(new Point(5,6));
        list.add(new Point(7,8));
        System.out.println("list:"+list);


        //比较点重写compare方法在Comparator接口中
        //使用匿名内部类
        Collections.sort(list,new A() );
        System.out.println("list->sort->compare:"+list);
        Collections.sort(list,(x,y)->{
            int len1=x.getX()*x.getX()+x.getY()* x.getY();
            int len2=y.getX()*y.getX()+y.getY()* y.getY();
            return len1-len2;
        });
    }

}
class A  implements  Comparator<Point>{


    @Override
    public int compare(Point o1, Point o2) {
        int len1=o1.getX()*o1.getX()+o1.getY()* o1.getY();
        int len2=o2.getX()*o2.getX()+o2.getY()* o2.getY();
        return len1-len2;
    }
}
